import { reqGoodsInfo,reqAddOrUpdateShopCart } from "@/api"
import { getUUID } from "@/utils/uuid_token"
const state = {
    goodInfo:{},
    uuid_token:getUUID()
}  
const mutations = {
    GETGOODINFO(state,goodInfo){
        state.goodInfo=goodInfo
   
    }
}
const actions = {
    async getGoodInfo({commit},skuId){
    let result=await reqGoodsInfo(skuId);
   
    
    if(result.code==200){
        commit('GETGOODINFO',result.data)
    }else {
        // 处理错误情况
        console.error('获取商品详情信息失败', result);
      };
    },
    //添加购物车
    async addOrUpdateShopCart({commit},{skuId,skuNum}){
        //加入购物车返回的结果
        let result=await reqAddOrUpdateShopCart(skuId,skuNum);
     //服务器写入数据成功，没有返回其他数据，只是返回了code=200，代表成功
        if(result.code==200){
            return 'ok'
        }else {
            //代表加入购物车失败
            return Promise.reject(new Error('faile'))
        }
       
       
    }

}
const getters = {
    //路径导航信息
    categoryView(state){
        return state.goodInfo.categoryView||{}
    },
    //产品信息
    skuInfo(state){
        return state.goodInfo.skuInfo||{}
    },
    //产品售卖属性
    spuSaleAttrList(state){
        return state.goodInfo.spuSaleAttrList||[]
    }
}
export default {
   
    state,
    mutations,
    actions,
    getters
}
